<?php
namespace app\admin\controller;

use app\admin\model\Message as MessageModel;
class Message extends Base
{
    public function index(){
        if(request()->isPost()) {
            $key=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list = db('message')
                ->where('name|tel|content', 'like', "%" . $key . "%")
                ->order('addtime desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:s',$v['addtime']);
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }

    /**
     * 删除留言
     * @param MessageModel $messageModel
     * @return array
     * @throws \Exception
     */
    public function del(MessageModel $messageModel){
        $message_id = $this->request->param('message_id');

        $map['message_id'] = $message_id;
        $res = $messageModel->where($map)->delete();
        if($res){
            return ['code'=>1,'msg'=>'删除成功'];
        }
        return ['code'=>0, 'msg'=>'删除失败'];
    }

    /**
     * 删除（多个）
     * @param MessageModel $messageModel
     * @return array
     * @throws \Exception
     */
    public function delAll(MessageModel $messageModel){
        $ids = $this->request->param('ids');

        $map['message_id'] =array('IN', $ids);
        $res = $messageModel->where($map)->delete();

        if($res){
            return ['msg' => '删除成功', 'code' => 1, 'url' => url('index')];
        }
        return ['msg' => '删除失败', 'code' => 0, 'url' => url('index')];
    }
}